package com.woniuxy.springboot.contorller;

import com.woniuxy.springboot.entity.Dep;
import com.woniuxy.springboot.entity.Emp;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 佘赛飞
 * @create 2021 -10 -09 6:28 AM
 */
@Controller
public class EmpController {
    //查询
    @GetMapping("/emps")
    public String getAllEmps(Model model){
        ArrayList<Emp> emps = new ArrayList<>();
        emps.add(new Emp(1,"zs",new Date(),
                 new Dep(1,"d001")));
        emps.add(new Emp(2,"ls",new Date(),
                 new Dep(2,"d002")));
        model.addAttribute("emps",emps);
        return "emps";
    }
    
    @GetMapping("/emp")
    public String getAllDeps(Model model){
        ArrayList<Dep> deps = new ArrayList<>();
        deps.add(new Dep(1,"d001"));
        deps.add(new Dep(2,"d002"));
        model.addAttribute("deps",deps);
        return "empadd";
    }
    //添加员工
    @PostMapping("/emp")
    public String addEmp(Emp emp){
        System.out.println("添加员工"+emp);
        //添加成功，去列表页面展示数据
        //这个可以进页面，同步没有走Controller没有数据
        //return "/emps.html";
        ////不能转发405    post---get
        return "redirect:/emps";
    }

    // 获取请求路径中的参数    /emp/1    /emp/2
    @GetMapping("/emp/{eid}")
    public String toEmpEdit(@PathVariable("eid")int eid, Model model ) {
        Emp emp=null;
        if(eid==1) {
            emp=new Emp(1, "zs", new Date(), new Dep(1, "d001"));
        }else {
            emp=new Emp(2, "ls", new Date(), new Dep(2, "d002"));
        }
        List<Dep> deps = new ArrayList<Dep>();
        deps.add(new Dep(1, "d001"));
        deps.add(new Dep(2, "d002"));
        model.addAttribute("deps", deps);
        model.addAttribute("emp", emp);
        //    /templates/empeidt.html
        return "/empedit.html";
    }
    //修改
    @PutMapping("/emp")
    public String editEmp(Emp emp) {
        System.out.println("修改员工：" + emp);
        return "redirect:/emps";
    }
}
